﻿import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Login
{
	//登入變數
	private String Name ;
	private int  Choose ;
	//檔案變數
	Scanner     inputStream  = null ;
	PrintWriter outputStream = null ;
	//當前使用者
	public User NowUser ;
	public Login( )
	{
		//Scanner
		Scanner scanner = new Scanner( System.in ) ;
		System.out.println( "========測試用登入系統========\n" ) ;
		//選擇登入或是創建新資料
		System.out.println( "輸入1登入或是2創建新資料：" ) ;
		Choose = scanner.nextInt( ) ;
		while( Choose != 1 && Choose != 2 )
		{
			System.out.println( "輸入數字有誤,請重新嘗試.." ) ;
			Choose = scanner.nextInt( ) ;
		}
		//使用者輸入暱稱
		System.out.println( "請輸入您的暱稱 ：（小於10個英文字元）" ) ;
		Name = scanner.next( ) ;
		while( CheckNameOK( Choose , Name ) == false )
		{
			if( Choose == 2 )
				System.out.println( "輸入暱稱已被使用,請嘗試新暱稱.." ) ;
			else
				System.out.println( "此暱稱不存在,請重新嘗試.." ) ;
			Name = scanner.next( ) ;
		}
		//登入
		if( Choose == 1 )
		{
			System.out.println( NowUser.getUserName( ) + "登入成功!!(1)" ) ;
			NowUser.ShowUserInfo( ) ;
		}
		//創建新資料
		else
		{
			NowUser = new User( Name ) ; 
			WriteData( NowUser ) ;
			System.out.println( NowUser.getUserName( ) + "登入成功!!(2)" ) ;
			NowUser.ShowUserInfo( ) ;
		}
		scanner.close( ) ;
		//Server 介面
	}
	public boolean CheckNameOK( int Chosen , String name )
	{
		//Name is illigal or not
		if( name.length( ) <= 0 && name.length( ) > 10 )
			return false ;
		//Is Login, check data
		//Checking Name is OK in the file or not
		//OK, Read User Infomation
		return ReadData( Chosen , name ) ;
	}
	//創建新使用者資料
	public synchronized void WriteData( User OkUser )
	{
		try
		{
			//取得檔案尾端
			outputStream = new PrintWriter( new FileOutputStream( "UserSet.txt" , true ) ) ;
		}
		catch( FileNotFoundException e )
		{
			JOptionPane.showMessageDialog( null, "WriteData　Error : AllMessage.txt 檔案不存在 請先建置！！" ) ;
			System.exit( 0 ) ;
		}
		//寫入最新一筆資訊
		outputStream.println( OkUser.getUserName( ) + "\t" + OkUser.getUserBuildTime( ) ) ;
		outputStream.close( ) ;
	}
	//讀取資料判定有無
	public boolean ReadData( int Chosen , String CheckName )
	{
		try 
		{
			inputStream = new Scanner( new FileInputStream( "UserSet.txt" ) ) ;		
		}
		catch( FileNotFoundException e )
		{
			System.out.println( "ReadData　Error : UserSet.txt 檔案有問題！！" ) ;
			System.exit( 0 ) ;
		}
		String A , B ;
		String [] Token ;
		String [] Save = new String [ 2 ] ;
		while( inputStream.hasNextLine( ) )
		{
			A = inputStream.nextLine( ) ;
			//一個使用者: "名稱\t創見日期\t"
			B = A ;
			Token = B.split( "\t" ) ;
			int i = 0 ;
			for( String Tk : Token )
			{
				Save[ i ] = Tk ;
				//Debugger..
				//System.out.println( "x" + i + Save[ i ] + "x" ) ;
				i++ ;
			}
			if( Chosen == 1 && Save[ 0 ].contentEquals( CheckName ) == true )
			{
				NowUser = new User( Save[ 0 ] , Save[ 1 ] ) ;
				inputStream.close( ) ;
				return true ;
			}
			else if( Chosen == 2 && Save[ 0 ].contentEquals( CheckName ) == true )
			{
				inputStream.close( ) ;
				return false ;
			}
		}
		inputStream.close( ) ;
		if( Chosen == 1 )
			return false ;
		else
			return true ;
	}
}
